﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Web.Areas.Administration.Models.Dictionary;
using AutoPartsSearch.Web.ViewModels;
using AutoPartsSearch.Web.ViewModels.SearchQuery;

namespace AutoPartsSearch.Web.App_Start
{
    public class AutoMapperConfig
    {
        public static void CreateMaps()
        {
            Mapper.CreateMap<int, Dictionary>().ConstructUsing(x => new Dictionary(x));

            Mapper.CreateMap<int?, Dictionary>().ConstructUsing(x => new Dictionary(x.Value));

            Mapper.CreateMap<AutoPartsSearch.Web.ViewModels.SellerModels.Register, Seller>()
                .ForMember(x => x.CarBrands, m => m.MapFrom(reg => reg.CarBrands.Select(brand => new Dictionary(brand))))
                .ForMember(x => x.PartGroups, m => m.MapFrom(reg => reg.PartGroups.Select(part => new Dictionary(part))))
                .ForMember(x => x.City, m => m.MapFrom(reg => new Dictionary(reg.City.Value)));

            Mapper.CreateMap<AutoPartsSearch.Web.ViewModels.AccountModels.SellerInfo, Seller>()
                .ForMember(x => x.CarBrands, m => m.MapFrom(reg => reg.CarBrands.Select(brand => new Dictionary(brand))))
                .ForMember(x => x.PartGroups, m => m.MapFrom(reg => reg.PartGroups.Select(part => new Dictionary(part))))
                .ForMember(x => x.City, m => m.MapFrom(reg => new Dictionary(reg.City.Value)));
            Mapper.CreateMap<Seller, AutoPartsSearch.Web.ViewModels.AccountModels.SellerInfo>()
                .ForMember(x => x.CarBrands, m => m.MapFrom(reg => reg.CarBrands.Select(brand => brand.Id)))
                .ForMember(x => x.PartGroups, m => m.MapFrom(reg => reg.PartGroups.Select(part => part.Id)))
                .ForMember(x => x.City, m => m.MapFrom(reg => reg.City.Id));


            Mapper.CreateMap<Seller, AutoPartsSearch.Web.Areas.Administration.Models.SellerAccount.ViewModel>()
               .ForMember(x => x.CarBrands, m => m.MapFrom(reg => reg.CarBrands.Select(brand => brand.Name.Default).Aggregate((current, next) => current + ", " + next)))
               .ForMember(x => x.PartGroups, m => m.MapFrom(reg => reg.PartGroups.Select(brand => brand.Name.Default).Aggregate((current, next) => current + ", " + next)))
               .ForMember(x => x.City, m => m.MapFrom(reg => reg.City.Name.Default));

            Mapper.CreateMap<BlackListAdd, BlackList>();

            Mapper.CreateMap<DictionaryModel, Dictionary>().ForMember(x => x.Name, m => m.MapFrom(loc => new Localization
            {
                Ru = loc.Ru,
                Ro = loc.Ro,
                En = loc.En
            }));
            Mapper.CreateMap<Dictionary, DictionaryModel>()
                .ForMember(x => x.Ru, m => m.MapFrom(dict => dict.Name.Ru))
                .ForMember(x => x.Ro, m => m.MapFrom(dict => dict.Name.Ro))
                .ForMember(x => x.En, m => m.MapFrom(dict => dict.Name.En))
                .ForMember(x => x.Id, m => m.MapFrom(dict => dict.Id));

            Mapper.CreateMap<SearchQueryAdd, SearchQuery>()
                .ForMember(sq => sq.PartGroup, options => options.MapFrom(sqa => new Dictionary(sqa.Parts[0].PartGroup.Value)))
                .ForMember(sq => sq.PartState, options => options.MapFrom(sqa => new Dictionary(sqa.Parts[0].PartState.Value)))
                .ForMember(sq => sq.CarBrand, options => options.MapFrom(sqa => new Dictionary(sqa.CarBrand.Value)))
                .ForMember(sq => sq.CarCase, options => options.MapFrom(sqa => new Dictionary(sqa.CarCase.Value)))
                .ForMember(sq => sq.CarDrive, options => options.MapFrom(sqa => new Dictionary(sqa.CarDrive.Value)))
                .ForMember(sq => sq.CarFuelType, options => options.MapFrom(sqa => new Dictionary(sqa.CarFuelType.Value)))
                .ForMember(sq => sq.CarTransmission, options => options.MapFrom(sqa => new Dictionary(sqa.CarTransmission.Value)))
                .ForMember(sq => sq.CarType, options => options.MapFrom(sqa => new Dictionary(sqa.CarType.Value)))
                .ForMember(sq => sq.City, options => options.MapFrom(sqa => new Dictionary(sqa.City)))
                .ForMember(sq => sq.PartType, options => options.MapFrom(sqa => new Dictionary(sqa.Parts[0].PartType.Value)));

            Mapper.CreateMap<SearchQueryAdd, List<SearchQuery>>().ConvertUsing<AutoMapperConverters.SearchQueryTypeConverter>();

            Mapper.CreateMap<ViewModels.Home.Contact, AutoPartsSearch.Domain.Entities.Contact>();

        }
    }
}